:- module(dump_database,[main/1]).

:- set_prolog_flag(discontiguous_warnings,off).
:- use_module(db(database_impl)).
:- include(mysql_impl).
:- set_prolog_flag(discontiguous_warnings,on).

main([]):-
	format("Usage: dump_database [mysql|persdb]~n",[]),
	format(" mysql     Dumps the MySql database into the persdb database;~n",[]),
	format("           current Amos database *must* be of type persdb~n",[]),
	format(" persdb    Dumps the persdb database into the MySql database;~n",[]),
	format("           current Amos database *must* be of type persdb~n",[]).
main([mysql]):-
	format("-------------------------------------------------------------~n",[]),
	format("Dumping data from Amos MySql database to persdb database.~n",[]),
	format("Remember that current Amos database *must* be of type persdb~n",[]),
	format("-------------------------------------------------------------~n",[]),
	dump_tables(mysql),
	database_impl:update_db,
	format("-------------------------------------------------------------~n",[]),
	format("Amos database dumped.~n",[]),
	format("-------------------------------------------------------------~n",[]).
main([persdb]):-
	format("-------------------------------------------------------------~n",[]),
	format("Dumping data from Amos persdb database to MySql database.~n",[]),
	format("Remember that current Amos database *must* be of type persdb~n",[]),
	format("-------------------------------------------------------------~n",[]),
	dump_tables(persdb),
	format("-------------------------------------------------------------~n",[]),
	format("Amos database dumped.~n",[]),
	format("-------------------------------------------------------------~n",[]).

dump_tables(Type):-
	dump_table(Type,identificationtags,3),
	dump_table(Type,requires,3),
	dump_table(Type,uses,3),
	dump_table(Type,cert_status,2),
	dump_table(Type,synonyms,2),
	dump_table(Type,generalizations,2),
	dump_table(Type,translations,3),
	dump_table(Type,authors,2),
	dump_table(Type,maintainers,2),

%	dump_table(Type,descriptions,2),
	dump_descriptions(Type),

	dump_table(Type,asset,19),
	dump_table(Type,resource,6),
	dump_table(Type,certification,10),
	dump_table(Type,ditem,3),
	dump_table(Type,organization,2),
	dump_table(Type,user,3).


dump_table(mysql,PredName,A):-
	functor(Goal,PredName,A),
	dbcurrent_fact(Goal),
	( 
	    internal_data(Goal) ->
	    insert(Goal)
	;
	    insert(_,Goal)
	),
	fail.
dump_table(persdb,PredName,A):-
	functor(Goal,PredName,A),
	( 
	    internal_data(Goal) ->
	    retrieve(Goal)
	;
	    retrieve(_,Goal)
	),
	dbassertz_fact(Goal),
	fail.
dump_table(_Type,PredName,A):-
	format("~w/~w dumped.~n",[PredName,A]).

dump_descriptions(mysql):-
	dump_table(mysql,descriptions,2).
dump_descriptions(persdb):-
	retrieve(descriptions(Key,Str)),
 display(Key),nl,display(Str),nl,
	atom_codes(Str,StrS),
	get_chunk(StrS,ChunkS,_),
	atom_codes(Chunk,ChunkS),
	dbassertz_fact(descriptions(Key,Chunk)),
	fail.
dump_descriptions(persdb):-
	format("descriptions/2 dumped.~n",[]).
	

get_chunk(Str,Chunk,Tail):-
	getN(100,Str,Chunk,Tail,_).
get_chunk(Str,Chunk,Tail):-
	getN(100,Str,_ChunkS0,Tail0,100),
	\+ Tail0 = [],
	get_chunk(Tail0,Chunk,Tail).

getN(0,Str,[],Str,0):- !.
getN(N,[],[],[],0):-
	N >= 0, !.
getN(N,[S|Str],[S|Chunk],Tail,M):-
	N > 0,
	N1 is N - 1,
	getN(N1,Str,Chunk,Tail,M1), 
	M is M1+1, !.

	



